<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import orderTabBg from 'assets/images/order-tab-bg.png';
    import listNullIcon from 'assets/images/list-null-icon.png';
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";

    const globalEvent = weex.requireModule('globalEvent');
    const dom = weex.requireModule('dom');

    export default{
        data() {
            return {
                orderTabBg: orderTabBg,
                listNullIcon: listNullIcon,
                typeId: 1, //默认全部订单 1全部订单 2待付款 3待发货 4待收货 5待评价 6退款退货
                orderList: [],
                start: 0,
                limit: 9,
                refEl: 'cell_0',//n起始值为0
                showGoTop: false,//默认置顶按钮为隐藏
                loading: false, //正在加载
                listLoaded: false, //默认的页面没有加载
                listCompleted: false,//分页完成
                tabList: [
                    {
                        name: '全部',
                        typeId: 1
                    },
                    {
                        name: '待付款',
                        typeId: 2
                    },
                    {
                        name: '待发货',
                        typeId: 3
                    },
                    {
                        name: '待收货',
                        typeId: 4
                    },
                    {
                        name: '待评价',
                        typeId: 5
                    },
                    {
                        name: '退款/退货',
                        typeId: 6
                    }
                ]
            }
        },
        created() {
            let self = this;
            let config = weex.config;
            if(config.typeId) self.typeId = config.typeId;

            self.sendHttp(res=>{
                self.pageInit(res);
            });

            //监听待付款订单支付成功事件
            globalEvent.addEventListener(constant.EVENT_ORDER_PAY_SUCCESS, function (e) {
                let _oid = e.data || {};
                self.orderList = self.orderList.filter(order=>{
                    if(order.transactionid == _oid) order.status = "待发货";
                    if(self.typeId == 1){
                        return order;
                    } else {
                        return order.transactionid != _oid;
                    }
                });
            });

            //监听订单详情事件
            globalEvent.addEventListener(constant.EVENT_ORDER_DETAIL, function (e) {
                let _data = e.data || {};
                self.updateOrder(_data);
            });

            //监听订单评价事件
            globalEvent.addEventListener(constant.EVENT_ORDER_RATE, function (e) {
                let _data = e.data || {};
                self.updateOrder(_data);
            });
        },
        methods: {
            getSelectList(tab){
                let self = this;
                if(self.typeId == tab.typeId) return;

                self.initData();
                self.typeId = tab.typeId;

                self.sendHttp(res=>{
                    self.pageInit(res);
                });
            },
            sendHttp(succCallback,failCallback){
                let self = this;
                let _options = {
                    typeid: self.typeId,
                    start: self.start,
                    limit: self.limit
                };
                api.sendHttp("myOrdersApi",_options).then(res=>{
                    if(typeof succCallback == "function") succCallback(res);
                },
                res=>{
                    modal.toast(res.msg);
                    if(typeof failCallback == "function") failCallback(res);
                });
            },
            pageInit(res){
                let self = this;
                self.listLoaded = true;
                self.orderList = self.orderList.slice(0,0).concat(res.data || []);

                //总数据量小于limit
                if(self.orderList.length < self.limit) {
                    self.listCompleted = true;
                }
            },
            initData(){
                let self = this;
                self.start = 0;
                self.listCompleted = false;
                self.listLoaded = false;

                //滚动条重置到顶部
                let el = self.$refs['cell-first'];
                if(el) dom.scrollToElement(el, {offset: 0, animated:false});
            },
            fetchMore(){
                this.$refs['list'].resetLoadmore(); //下一次滚动时强制触发loadmore

                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }

                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                self.sendHttp(res=>{
                    self.loading = false;
                    let _orderList = res.data || [];

                    if(_orderList.length < self.limit) self.listCompleted = true;
                    self.orderList = self.orderList.concat(_orderList);
                },res=>{
                    self.loading = false;
                });
            },
            onScroll(e) {
                let contentOffset = e.contentOffset;
                let offsetY = contentOffset.y;
                if(offsetY <= -4000){
                    if(!this.showGoTop) this.showGoTop = true;
                } else if(offsetY > -4000) {
                    if(this.showGoTop) this.showGoTop = false;
                }
            },
            goToRefundDetail(order){
                let options = {
                    classType: constant.Drawer_Class_Type,
                    url: route.order_funddetail,
                    title: "退款详情",
                    params: {
                        rid: order.rid
                    }
                };
                navigator.push(options);
            },
            delOrder(order){
                let self = this;
                self.orderList = self.orderList.filter(o=>{
                    return o.transactionid != order.transactionid;
                });
            },
            updateOrder(order){
                let self = this;
                self.orderList = self.orderList.filter(o=>{
                    if(order.status == "已删除"){
                        return o.transactionid != order.transactionid;
                    }

                    if(self.typeId == 1){
                        if(o.transactionid == order.transactionid) Object.assign(o,order);
                        return o;
                    }

                    if(o.can_delay != 0 && order.can_delay == 0){ //延长收货
                        if(o.transactionid == order.transactionid) Object.assign(o,order);
                        return o;
                    }

                    return o.transactionid != order.transactionid;
                });
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_ORDER_PAY_SUCCESS);
            globalEvent.removeEventListener(constant.EVENT_ORDER_DETAIL);
            globalEvent.removeEventListener(constant.EVENT_ORDER_RATE);
        }
    }
</script>
